package com.xhd.db;

import java.util.Vector;
import com.xhd.base.util.xBaseObj;
import com.xhd.util.DataFormat;

import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;


public class DBQuery 
{     private String jm443 = null ;
	  private Vector jm444 = new Vector();
	  private Vector jm445 = new Vector();
	  private Vector jm446 = new Vector();
	  private int jm447 = 0;
	  private int jm448 = 0;
	  private int jm449 = -1;
	  private Vector jm450 = null;
	  private Vector jm451 = null;
	  private String jm452 = null;
	  private Vector jm453 = null;
	  private boolean jm454 = false;
	  private String jm455 = null;
	  private boolean jm456 = false;
	  public static final int RESULT_SELECT = -1;
	  public static final int RESULT_NONE = 0;
	  public static final int RESULT_STRING = 1;
	  public static final int RESULT_SET = 2;

	  protected DBQuery()
	  {
	  }

	  protected void setSQL(String paramString)
	  {
	    this.jm443 = paramString;
	  }

	  public DBQuery(String paramString)
	  {
	    this.jm443 = paramString;
	    if ((paramString == null) || (paramString.length() <= 7) || (!("select ".equals(paramString.substring(0, 7).toLowerCase()))))
	      return;
	    Vector localVector = SQLAnalyzer.splitUnionSQL(paramString);
	    if (localVector.size() > 1)
	    {
	      SQLAnalyzer.sqlToQuery((String)localVector.get(0), this);
	      for (int i = 1; i < localVector.size(); ++i)
	      {
	        DBQuery localDBQuery = new DBQuery();
	        SQLAnalyzer.sqlToQuery((String)localVector.get(i), localDBQuery);
	        union(localDBQuery);
	      }
	    }
	    else
	    {
	      SQLAnalyzer.sqlToQuery(paramString, this);
	    }
	  }

	  public DBQuery(xBaseObj paramxBaseObj, String paramString)
	  {
	    if (paramString == null)
	      paramString = "*";
	    addTable(paramxBaseObj.getClassName(), paramString);
	  }

	  public DBQuery(xBaseObj paramxBaseObj, String paramString, DBWhereExp paramDBWhereExp)
	  {
	    if (paramString == null)
	      paramString = "*";
	    addTable(paramxBaseObj.getClassName(), paramString);
	    andExp(paramDBWhereExp);
	  }

	  public DBQuery(String paramString1, String paramString2)
	  {
	    if (paramString2 == null)
	      paramString2 = "*";
	    addTable(paramString1.trim(), paramString2);
	  }

	  public DBQuery(String paramString1, String paramString2, DBWhereExp paramDBWhereExp)
	  {
	    if (paramString2 == null)
	      paramString2 = "*";
	    addTable(paramString1.trim(), paramString2);
	    andExp(paramDBWhereExp);
	  }

	  public void setMaxCount(int paramInt)
	  {
	    this.jm448 = paramInt;
	  }

	  public void setIsProcedure(int paramInt)
	  {
	    this.jm449 = paramInt;
	  }

	  public boolean isProcedure()
	  {
	    return (this.jm449 == 2);
	  }

	  public void setCallParams(Collection paramCollection)
	  {
	    if (this.jm449 == -1)
	      this.jm449 = 2;
	    if ((paramCollection == null) || (paramCollection.size() <= 0))
	      return;
	    this.jm450 = new Vector(paramCollection.size());
	    this.jm450.addAll(paramCollection);
	  }

	  public List getCallParams()
	  {
	    return this.jm450;
	  }

	  public int getMaxCount()
	  {
	    return this.jm448;
	  }

	  public int getPage()
	  {
	    return this.jm447;
	  }

	  public void setPage(int paramInt)
	  {
	    this.jm447 = paramInt;
	  }

	  public String getMainTable()
	  {
	    if (this.jm445.size() > 0)
	    {
	      String str = (String)this.jm445.get(0);
	      int i = str.indexOf(32);
	      if (i > 0)
	        str = str.substring(0, i);
	      return str;
	    }
	    return SQLAnalyzer.getTable(this.jm443);
	  }

	  public Vector getTables(boolean paramBoolean)
	  {
	    if (this.jm445.size() > 0)
	    {
	      int i = this.jm445.size();
	      Vector localVector = new Vector(i);
	      int j = 0;
	      int k = 0;
	     
	      while (true)
	      {
	        String str = (String)this.jm445.get(k);
	        if (str.indexOf(44) > 0)
	        {
	          String[] arrayOfString = DataFormat.splitString(str, ',');
	          for (int l = 0; l < arrayOfString.length; ++l)
	          {
	            arrayOfString[l] = arrayOfString[l].trim();
	            if (!(paramBoolean))
	              j = arrayOfString[l].indexOf(32);
	            else
	              j = -1;
	            if (j > 0)
	              localVector.add(arrayOfString[l].substring(0, j));
	            else
	              localVector.add(arrayOfString[l]);
	          }
	        }
	        else
	        {
	          str = str.trim();
	          if (!(paramBoolean))
	            j = str.indexOf(32);
	          else
	            j = -1;
	          if (j > 0)
	            localVector.add(str.substring(0, j));
	          else
	            localVector.add(str);
	        }
	        ++k;
	        if (k >= i)
	          label215: return localVector;
	      }
	    }
	    return SQLAnalyzer.getTables(this.jm443, paramBoolean);
	  }

	  public void andExp(DBWhereExp paramDBWhereExp)
	  {
	    if (paramDBWhereExp == null)
	      return;
	    this.jm444.add(paramDBWhereExp);
	  }

	  public void orExp(DBWhereExp paramDBWhereExp)
	  {
	    if (paramDBWhereExp == null)
	      return;
	    paramDBWhereExp.setAnd(false);
	    this.jm444.add(paramDBWhereExp);
	  }

	  public DBWhereExp getWhere()
	  {
	    int i = this.jm444.size();
	    if (i < 1)
	      return null;
	    if (1 == i)
	      return ((DBWhereExp)this.jm444.get(0));
	    DBWhereExp localDBWhereExp1 = new DBWhereExp(null);
	    for (int j = 0; j < i; ++j)
	    {
	      DBWhereExp localDBWhereExp2 = (DBWhereExp)this.jm444.get(j);
	      if (localDBWhereExp2.isAnd())
	        localDBWhereExp1.and(localDBWhereExp2);
	      else
	        localDBWhereExp1.or(localDBWhereExp2);
	    }
	    return localDBWhereExp1;
	  }

	  public void clearWhere()
	  {
	    this.jm444.clear();
	  }

	  public int getResultType()
	  {
	    return this.jm449;
	  }

	  public void leftJoin(String paramString, DBWhereExp paramDBWhereExp)
	  {
	    if (this.jm451 == null)
	      this.jm451 = new Vector();
	    Object[] arrayOfObject = new Object[3];
	    arrayOfObject[0] = "left";
	    arrayOfObject[1] = paramString;
	    arrayOfObject[2] = paramDBWhereExp;
	    this.jm451.add(arrayOfObject);
	  }

	  public void rightJoin(String paramString, DBWhereExp paramDBWhereExp)
	  {
	    if (this.jm451 == null)
	      this.jm451 = new Vector();
	    Object[] arrayOfObject = new Object[3];
	    arrayOfObject[0] = "right";
	    arrayOfObject[1] = paramString;
	    arrayOfObject[2] = paramDBWhereExp;
	    this.jm451.add(arrayOfObject);
	  }

	  public void fullJoin(String paramString, DBWhereExp paramDBWhereExp)
	  {
	    if (this.jm451 == null)
	      this.jm451 = new Vector();
	    Object[] arrayOfObject = new Object[3];
	    arrayOfObject[0] = "full";
	    arrayOfObject[1] = paramString;
	    arrayOfObject[2] = paramDBWhereExp;
	    this.jm451.add(arrayOfObject);
	  }

	  public void setOrder(String paramString)
	  {
	    this.jm452 = paramString;
	  }

	  public void setOrder(String paramString, boolean paramBoolean)
	  {
	    this.jm452 = paramString;
	    if (paramBoolean)
	      return;
	    this.jm452 += " DESC";
	  }

	  public void setGroupBy(String paramString)
	  {
	    this.jm455 = paramString;
	  }

	  public void setDistinct(boolean paramBoolean)
	  {
	    this.jm454 = paramBoolean;
	  }

	  public void union(DBQuery paramDBQuery)
	  {
	    if (this.jm453 == null)
	      this.jm453 = new Vector();
	    this.jm453.add(paramDBQuery);
	  }

	  public void unionAll(DBQuery paramDBQuery)
	  {
	    if (this.jm453 == null)
	      this.jm453 = new Vector();
	    this.jm453.add(paramDBQuery);
	  }

	  public String toSQL(int paramInt)
	  {
	    StringBuffer localStringBuffer = new StringBuffer(256);
	    int i = 0;
	    Object localObject1 = null;
	    int j;
	    Object localObject2[];
	    if (this.jm443 != null)
	    {
	      localStringBuffer.append(this.jm443);
	      i = (SQLAnalyzer.getKeyIndex(this.jm443, "where ") > 0) ? 1 : 0;
	    }
	    else if (this.jm449 == -1)
	    {
	      localStringBuffer.append("SELECT ");
	      if (this.jm454)
	        localStringBuffer.append("DISTINCT ");
	      if (this.jm446.size() > 0)
	        for (j = 0; j < this.jm446.size(); ++j)
	        {
	          if (j > 0)
	            localStringBuffer.append(",");
	          localStringBuffer.append(this.jm446.get(j));
	        }
	      else
	        localStringBuffer.append("*");
	      localStringBuffer.append(" FROM ");
	      for (j = 0; j < this.jm445.size(); ++j)
	      {
	        if (j > 0)
	          localStringBuffer.append(",");
	        localStringBuffer.append(this.jm445.get(j));
	      }
	      if (this.jm451 != null)
	        for (j = 0; j < this.jm451.size(); ++j)
	        {
	          localObject2 = (Object[])this.jm451.get(j);
	          String str = ((DBWhereExp)localObject2[2]).toSQL(paramInt);
	          if (paramInt == 1)
	          {
	            localStringBuffer.append(",");
	            localStringBuffer.append(localObject2[1]);
	            if ("left".equals(localObject2[0]))
	            {
	              str = str + "(+)";
	              if (str.indexOf(32) > 0)
	              {
	                str = str.replaceAll(" AND ", "(+) AND ");
	                str = str.replaceAll(" OR ", "(+) OR ");
	              }
	            }
	            else if ("right".equals(localObject2[0]))
	            {
	              str = str.replaceAll("=", "(+)=");
	            }
	            else
	            {
	              str = str.replaceAll("=", "(+)=") + "(+)";
	              if (str.indexOf(32) > 0)
	              {
	                str = str.replaceAll(" AND ", "(+) AND ");
	                str = str.replaceAll(" OR ", "(+) OR ");
	              }
	            }
	            if (localObject1 == null)
	              localObject1 = str;
	            else
	              localObject1 = localObject1 + " and " + str;
	          }
	          else
	          {
	            if ("left".equals(localObject2[0]))
	              localStringBuffer.append(" LEFT JOIN ");
	            else if ("right".equals(localObject2[0]))
	              localStringBuffer.append(" RIGHT JOIN ");
	            else
	              localStringBuffer.append(" FULL JOIN ");
	            localStringBuffer.append(localObject2[1]);
	            localStringBuffer.append(" ON ");
	            localStringBuffer.append(str);
	          }
	        }
	    }
	    if (this.jm449 == -1)
	    {
	      if (localObject1 != null)
	      {
	        if (i == 0)
	          localStringBuffer.append(" WHERE ");
	        else
	          localStringBuffer.append(" AND ");
	        localStringBuffer.append('(');
	        localStringBuffer.append((String)localObject1);
	        localStringBuffer.append(')');
	        i = 1;
	      }
	      for (j = 0; j < this.jm444.size(); ++j)
	      {
	        localObject2 = (Object[])this.jm444.get(j);
	        if (((DBWhereExp)localObject2[0]).isNull())
	          continue;
	        if (i == 0)
	        {
	          i = 1;
	          localStringBuffer.append(" WHERE ");
	        }
	        else
	        {
	          localStringBuffer.append((((DBWhereExp)localObject2[0]).isAnd()) ? " AND " : " OR ");
	        }
	        localStringBuffer.append(((DBWhereExp)localObject2[0]).toSQL(paramInt));
	      }
	    }
	    if (this.jm453 != null)
	      for (j = 0; j < this.jm453.size(); ++j)
	      {
	        localStringBuffer.append(" UNION ");
	        localStringBuffer.append(((DBQuery)this.jm453.get(j)).toSQL(paramInt));
	      }
	    if (this.jm455 != null)
	    {
	      localStringBuffer.append(" GROUP BY ");
	      localStringBuffer.append(this.jm455);
	    }
	    if (this.jm452 != null)
	    {
	      localStringBuffer.append(" ORDER BY ");
	      localStringBuffer.append(this.jm452);
	    }
	    return ((String)(String)localStringBuffer.toString());
	  }

	  private void jm442(DBSQL paramDBSQL, int paramInt)
	  {
	    int i = 0;
	    Object localObject1 = null;
	    int j;
	    Object localObject2[] = null;
	    if (this.jm443 != null)
	    {
	      paramDBSQL.appendSQL(this.jm443);
	      i = (SQLAnalyzer.getKeyIndex(this.jm443, "where ") > 0) ? 1 : 0;
	    }
	    else if (this.jm449 == -1)
	    {
	      paramDBSQL.appendSQL("SELECT ");
	      if (this.jm454)
	        paramDBSQL.appendSQL("DISTINCT ");
	      if (this.jm446.size() > 0)
	        for (j = 0; j < this.jm446.size(); ++j)
	        {
	          if (j > 0)
	            paramDBSQL.appendSQL(",");
	          paramDBSQL.appendSQL(this.jm446.get(j).toString());
	        }
	      else
	        paramDBSQL.appendSQL("*");
	      paramDBSQL.appendSQL(" FROM ");
	      for (j = 0; j < this.jm445.size(); ++j)
	      {
	        if (j > 0)
	          paramDBSQL.appendSQL(",");
	        paramDBSQL.appendSQL(this.jm445.get(j).toString());
	      }
	      if (this.jm451 != null)
	        for (j = 0; j < this.jm451.size(); ++j)
	        {
	          localObject2 = (Object[])this.jm451.get(j);
	          String str = ((DBWhereExp)localObject2[2]).toSQL(paramInt);
	          if (paramInt == 1)
	          {
	            paramDBSQL.appendSQL(",");
	            paramDBSQL.appendSQL(localObject2[1].toString());
	            if ("left".equals(localObject2[0]))
	            {
	              str = str + "(+)";
	              if (str.indexOf(32) > 0)
	              {
	                str = str.replaceAll(" AND ", "(+) AND ");
	                str = str.replaceAll(" OR ", "(+) OR ");
	              }
	            }
	            else if ("right".equals(localObject2[0]))
	            {
	              str = str.replaceAll("=", "(+)=");
	            }
	            else
	            {
	              str = str.replaceAll("=", "(+)=") + "(+)";
	              if (str.indexOf(32) > 0)
	              {
	                str = str.replaceAll(" AND ", "(+) AND ");
	                str = str.replaceAll(" OR ", "(+) OR ");
	              }
	            }
	            if (localObject1 == null)
	              localObject1 = str;
	            else
	              localObject1 = localObject1 + " and " + str;
	          }
	          else
	          {
	            if ("left".equals(localObject2[0]))
	              paramDBSQL.appendSQL(" LEFT JOIN ");
	            else if ("right".equals(localObject2[0]))
	              paramDBSQL.appendSQL(" RIGHT JOIN ");
	            else
	              paramDBSQL.appendSQL(" FULL JOIN ");
	            paramDBSQL.appendSQL(localObject2[1].toString());
	            paramDBSQL.appendSQL(" ON ");
	            paramDBSQL.appendSQL(str);
	          }
	        }
	    }
	    if (this.jm449 == -1)
	    {
	      if (localObject1 != null)
	      {
	        if (i == 0)
	          paramDBSQL.appendSQL(" WHERE ");
	        else
	          paramDBSQL.appendSQL(" AND ");
	        paramDBSQL.appendSQL("(");
	        paramDBSQL.appendSQL((String)localObject1);
	        paramDBSQL.appendSQL(")");
	        i = 1;
	      }
	      for (j = 0; j < this.jm444.size(); ++j)
	      {
	        localObject2[0] = (DBWhereExp)this.jm444.get(j);
	        if (((DBWhereExp)localObject2[0]).isNull())
	          continue;
	        if (i == 0)
	        {
	          i = 1;
	          paramDBSQL.appendSQL(" WHERE ");
	        }
	        else
	        {
	          paramDBSQL.appendSQL((((DBWhereExp)localObject2[0]).isAnd()) ? " AND " : " OR ");
	        }
	        paramDBSQL.append(paramInt, (DBWhereExp)localObject2[0]);
	      }
	    }
	    if (this.jm453 != null)
	      for (j = 0; j < this.jm453.size(); ++j)
	      {
	        paramDBSQL.appendSQL(" UNION ");
	        ((DBQuery)this.jm453.get(j)).jm442(paramDBSQL, paramInt);
	      }
	    if (this.jm455 != null)
	    {
	      paramDBSQL.appendSQL(" GROUP BY ");
	      paramDBSQL.appendSQL(this.jm455);
	    }
	    if (this.jm452 == null)
	      return;
	    paramDBSQL.appendSQL(" ORDER BY ");
	    paramDBSQL.appendSQL(this.jm452);
	  }

	  public DBSQL jm442(int paramInt)
	  {
	    DBSQL localDBSQL = new DBSQL();
	    jm442(localDBSQL, paramInt);
	    return localDBSQL;
	  }

	  public void addFields(String paramString)
	  {
	    if (paramString == null)
	      return;
	    this.jm446.add(paramString);
	  }

	  public void addFields(String paramString1, String paramString2)
	  {
	    if (paramString2 == null)
	      return;
	    if (paramString1 == null)
	    {
	      this.jm446.add(paramString2);
	    }
	    else
	    {
	      StringTokenizer localStringTokenizer = new StringTokenizer(paramString2, ",");
	      while (localStringTokenizer.hasMoreTokens())
	        this.jm446.add(paramString1 + "." + localStringTokenizer.nextToken());
	    }
	  }

	  public String addTable(String paramString)
	  {
	    int i = paramString.toLowerCase().indexOf(" as ");
	    if (i > 0)
	      paramString = paramString.substring(0, i) + paramString.substring(i + 4);
	    this.jm445.add(paramString);
	    return paramString;
	  }

	  public void addTable(String paramString1, String paramString2)
	  {
	    paramString1 = addTable(paramString1);
	    if (paramString2 == null)
	      return;
	    if (paramString2.indexOf(46) > 0)
	    {
	      addFields(paramString2);
	    }
	    else if (paramString1.indexOf(44) > 0)
	    {
	      addFields(paramString2);
	    }
	    else
	    {
	      int i = paramString1.lastIndexOf(32);
	      if (i > 0)
	        addFields(paramString1.substring(i + 1), paramString2);
	      else
	        addFields(paramString2);
	    }
	  }

	  public Vector getAllQuery()
	  {
	    int i = 1;
	    if (this.jm453 != null)
	      i += this.jm453.size();
	    Vector localVector = new Vector(i);
	    localVector.add(this);
	    if (this.jm453 != null)
	      localVector.addAll(this.jm453);
	    return localVector;
	  }

	  public boolean isIgnoreDaoRule()
	  {
	    return this.jm456;
	  }

	  public void setIgnoreDaoRule(boolean paramBoolean)
	  {
	    this.jm456 = paramBoolean;
	  }
}
